package com.ruoyi.project.jiaqin.advertplatform.service.impl;

import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.jiaqin.advertplatform.domain.JiaqinAdvertPlatform;
import com.ruoyi.project.jiaqin.advertplatform.mapper.JiaqinAdvertPlatformMapper;
import com.ruoyi.project.jiaqin.advertplatform.service.IJiaqinAdvertPlatformService;
import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.mapper.CompanyMapper;
import com.ruoyi.project.jiaqin.news.domain.News;
import com.ruoyi.project.jiaqin.news.mapper.NewsMapper;
import com.ruoyi.project.jiaqin.supply.domain.Supply;
import com.ruoyi.project.jiaqin.supply.mapper.SupplyMapper;
import com.ruoyi.project.system.goods.goods.domain.GoodsGoods;
import com.ruoyi.project.system.goods.goods.mapper.GoodsGoodsMapper;
import com.ruoyi.project.system.goods.product.domain.GoodsProduct;
import com.ruoyi.project.system.goods.product.mapper.GoodsProductMapper;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.mapper.MemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 平台投放视频、文章广告Service业务层处理
 *
 * @author LCL
 * @date 2022-01-08
 */
@Service
public class JiaqinAdvertPlatformServiceImpl implements IJiaqinAdvertPlatformService
{
    @Autowired
    private JiaqinAdvertPlatformMapper jiaqinAdvertPlatformMapper;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private SupplyMapper supplyMapper;
    @Autowired
    private GoodsGoodsMapper goodsGoodsMapper;
    @Autowired
    private GoodsProductMapper goodsProductMapper;
    @Autowired
    private NewsMapper newsMapper;

    /**
     * 查询平台投放视频、文章广告
     *
     * @param id 平台投放视频、文章广告ID
     * @return 平台投放视频、文章广告
     */
    @Override
    public JiaqinAdvertPlatform selectJiaqinAdvertPlatformById(String id)
    {
        return jiaqinAdvertPlatformMapper.selectJiaqinAdvertPlatformById(id);
    }

    /**
     * 查询平台投放视频、文章广告列表
     *
     * @param jiaqinAdvertPlatform 平台投放视频、文章广告
     * @return 平台投放视频、文章广告
     */
    @Override
    public List<JiaqinAdvertPlatform> selectJiaqinAdvertPlatformList(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        return jiaqinAdvertPlatformMapper.selectJiaqinAdvertPlatformList(jiaqinAdvertPlatform);
    }

    /**
     * 新增平台投放视频、文章广告
     *
     * @param jiaqinAdvertPlatform 平台投放视频、文章广告
     * @return 结果
     */
    @Override
    public AjaxResult insertJiaqinAdvertPlatform(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        String id = UUID.randomUUID().toString().replace("-","");
        jiaqinAdvertPlatform.setId(id);
        jiaqinAdvertPlatform.setCreatedTime(new Date());
        jiaqinAdvertPlatform.setDelFlag(0);
        News news = newsMapper.selectNewsById(jiaqinAdvertPlatform.getResId());
        if (news != null) {
            jiaqinAdvertPlatform.setResName(news.getTitle());
        }
        JiaqinAdvertPlatform advertPlatform = jiaqinAdvertPlatformMapper.selectJiaqinAdvertPlatformByResId(jiaqinAdvertPlatform.getResId());
        if (advertPlatform != null) {
           return AjaxResult.error("该新闻已有广告");
        }
        if (jiaqinAdvertPlatform.getType() == 1) {
            Member member = memberMapper.selectMemberById(jiaqinAdvertPlatform.getJumpLink());
            if (member != null && member.getUsername() != null && member.getHeadpic() != null) {
                jiaqinAdvertPlatform.setTitle(member.getUsername());
                jiaqinAdvertPlatform.setImgUrl(member.getHeadpic());
            }
        } else if (jiaqinAdvertPlatform.getType() == 2){
            Company company = companyMapper.selectCompanyByMemberId(jiaqinAdvertPlatform.getJumpLink());
            if (company != null && company.getCompanyName() != null && company.getCompanyAvatar() != null) {
                jiaqinAdvertPlatform.setTitle(company.getCompanyName());
                jiaqinAdvertPlatform.setImgUrl(company.getCompanyAvatar());
            }
        } else if (jiaqinAdvertPlatform.getType() == 3){
            Supply supply = supplyMapper.selectSupplyById(jiaqinAdvertPlatform.getJumpLink());
            if (supply != null && supply.getTitle() != null && supply.getImgUrls() != null && supply.getPrice() != null) {
                jiaqinAdvertPlatform.setTitle(supply.getTitle());
                String[] split = supply.getImgUrls().split(",");
                jiaqinAdvertPlatform.setImgUrl(split[0]);
                jiaqinAdvertPlatform.setPrice(supply.getPrice());
            }
        } else if (jiaqinAdvertPlatform.getType() == 4){
            GoodsGoods goodsGoods = goodsGoodsMapper.selectGoodsGoodsById(jiaqinAdvertPlatform.getJumpLink());
            if (goodsGoods != null && goodsGoods.getName() != null) {
                jiaqinAdvertPlatform.setTitle(goodsGoods.getName());
                if (goodsGoods.getMainUrl() != null) {
                    jiaqinAdvertPlatform.setImgUrl(goodsGoods.getMainUrl());
                }
                GoodsProduct goodsProduct = new GoodsProduct();
                goodsProduct.setGoodsId(goodsGoods.getId());
                goodsProduct.setIsDefault(1);
                List<GoodsProduct> productList = goodsProductMapper.selectGoodsProductList(goodsProduct);
                if (productList.size() > 0) {
                    jiaqinAdvertPlatform.setPrice(productList.get(0).getPriceN().toString());
                }
            }
        } else {
            jiaqinAdvertPlatform.setJumpLink(id);
        }
        jiaqinAdvertPlatformMapper.insertJiaqinAdvertPlatform(jiaqinAdvertPlatform);
        return AjaxResult.success();
    }

    /**
     * 修改平台投放视频、文章广告
     *
     * @param jiaqinAdvertPlatform 平台投放视频、文章广告
     * @return 结果
     */
    @Override
    public AjaxResult updateJiaqinAdvertPlatform(JiaqinAdvertPlatform jiaqinAdvertPlatform)
    {
        News news = newsMapper.selectNewsById(jiaqinAdvertPlatform.getResId());
        if (news != null) {
            jiaqinAdvertPlatform.setResName(news.getTitle());
        }
        if (jiaqinAdvertPlatform.getType() == 1) {
            Member member = memberMapper.selectMemberById(jiaqinAdvertPlatform.getJumpLink());
            if (member != null && member.getUsername() != null && member.getHeadpic() != null) {
                jiaqinAdvertPlatform.setTitle(member.getUsername());
                jiaqinAdvertPlatform.setImgUrl(member.getHeadpic());
            }
        } else if (jiaqinAdvertPlatform.getType() == 2){
            Company company = companyMapper.selectCompanyByMemberId(jiaqinAdvertPlatform.getJumpLink());
            if (company != null && company.getCompanyName() != null && company.getCompanyAvatar() != null) {
                jiaqinAdvertPlatform.setTitle(company.getCompanyName());
                jiaqinAdvertPlatform.setImgUrl(company.getCompanyAvatar());
            }
        } else if (jiaqinAdvertPlatform.getType() == 3){
            Supply supply = supplyMapper.selectSupplyById(jiaqinAdvertPlatform.getJumpLink());
            if (supply != null && supply.getTitle() != null && supply.getImgUrls() != null && supply.getPrice() != null) {
                jiaqinAdvertPlatform.setTitle(supply.getTitle());
                String[] split = supply.getImgUrls().split(",");
                jiaqinAdvertPlatform.setImgUrl(split[0]);
                jiaqinAdvertPlatform.setPrice(supply.getPrice());
            }
        } else if (jiaqinAdvertPlatform.getType() == 4){
            GoodsGoods goodsGoods = goodsGoodsMapper.selectGoodsGoodsById(jiaqinAdvertPlatform.getJumpLink());
            if (goodsGoods != null && goodsGoods.getName() != null) {
                jiaqinAdvertPlatform.setTitle(goodsGoods.getName());
                if (goodsGoods.getMainUrl() != null) {
                    jiaqinAdvertPlatform.setImgUrl(goodsGoods.getMainUrl());
                }
                GoodsProduct goodsProduct = new GoodsProduct();
                goodsProduct.setGoodsId(goodsGoods.getId());
                goodsProduct.setIsDefault(1);
                List<GoodsProduct> productList = goodsProductMapper.selectGoodsProductList(goodsProduct);
                if (productList.size() > 0) {
                    jiaqinAdvertPlatform.setPrice(productList.get(0).getPriceN().toString());
                }
            }
        }
        jiaqinAdvertPlatformMapper.updateJiaqinAdvertPlatform(jiaqinAdvertPlatform);
        return AjaxResult.success();
    }

    /**
     * 删除平台投放视频、文章广告对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinAdvertPlatformByIds(String ids)
    {
        return jiaqinAdvertPlatformMapper.deleteJiaqinAdvertPlatformByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除平台投放视频、文章广告信息
     *
     * @param id 平台投放视频、文章广告ID
     * @return 结果
     */
    @Override
    public int deleteJiaqinAdvertPlatformById(String id)
    {
        return jiaqinAdvertPlatformMapper.deleteJiaqinAdvertPlatformById(id);
    }

    @Override
    public JiaqinAdvertPlatform selectJiaqinAdvertPlatformByResId(String resId) {
        return jiaqinAdvertPlatformMapper.selectJiaqinAdvertPlatformByResId(resId);
    }
}
